'use strict';
swan_pc
.run(
    function($rootScope, $location, $window, $http, $state, $translate, Auth,$timeout){
        $rootScope.photoPrefixString = "http://www.diamondtiming.com:8080";

        $rootScope.$on('$stateChangeStart', function (event, toState, toStateParams) {
            $rootScope.toState = toState;
            $rootScope.toStateParams = toStateParams;
            Auth.hasAccount().then(function(){
                if ($rootScope.toState.name!= 'login'&& $rootScope.toState.name != 'activate'){
                    if (!Auth.isLoggedIn()) {
                        $timeout(function(){$state.go("login")});
                    }
                }
            })
        });

        $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
        });

        $rootScope.back = function() {
            // If previous state is 'activate' or do not exist go to 'home'
            if ($rootScope.previousStateName === 'activate' || $state.get($rootScope.previousStateName) === null) {
                $state.go('home');
            } else {
                $state.go($rootScope.previousStateName, $rootScope.previousStateParams);
            }
        };
        $rootScope.initDirtyCheck=function(){
            $rootScope.contactDirty=false;
            $rootScope.calendarDirty=false;
        };
        $rootScope.initDirtyCheck();
        $rootScope.checkDirty=function(){
            //contact dirty
            //console.log("contact:"+$rootScope.calendarDirty);
            if($rootScope.contactDirty) {
                if(!confirm("有数据未提交，确定要放弃修改联系人么?")) return false;
                $rootScope.contactDirty=false;
            }
            if($rootScope.companyDirty) {
                if(!confirm("有数据未提交，确定要放弃修改客户么?")) return false;
                $rootScope.companyDirty=false;
            }
            if($rootScope.calendarDirty){
                if(!confirm("有数据未提交，确定要放弃修改日程吗？"))return false;
                $rootScope.calendarDirty=false;
            }
            return true;
        };
        $rootScope.goSref=function(state){
            //console.log("--"+$rootScope.toState.name+"--"+state+"--"+$rootScope.checkDirty() );
            if ($rootScope.toState.name==state||$rootScope.checkDirty()==false) return false;
            else $state.go(state);
            return true;
        };
    }

);
